Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow any key in Renderer environment hash #26786

Merged
merged 1 commit into from Oct 15, 2016
Merged

Conversation

codeodor
Copy link
Contributor

@codeodor codeodor commented Oct 13, 2016

When rendering arbitrary templates, it would be helpful to not overwrite env keys with nil if they don't match any found in the RACK_KEY_TRANSLATION hash.

Is there any interest in this?

@rails-bot
Copy link

r? @eileencodes

(@rails-bot has picked a reviewer for you, use r? to override)

@rafaelfranca
Copy link
Member

Depends, could you show the use case for this? Right now I don't see why people would want this.

@codeodor
Copy link
Contributor Author

codeodor commented Oct 13, 2016

In my case, I want to pass a key for Warden so that my templates will not throw errors.

But more generally, I was rather surprised that it converted my keys to nil instead of just passing them through. I would imagine there are an infinite number of ways that view templates might rely on the environment outside of those predefined keys in the RACK_KEY_TRANSLATION hash.

@codeodor
Copy link
Contributor Author

I think a better way to go about it might be how it handles the values though:

https://github.com/codeodor/rails/blob/bfc6fbb0004694f94a3679176c4a3e58f3ca8a97/actionpack/lib/action_controller/renderer.rb#L108

@codeodor
Copy link
Contributor Author

More specifically, if I do something like this:

warden_manager = "set up warden"
SomeController.renderer.new(warden: warden_manager).render

Then in the template, if we check request.env['warden'] it is currently nil. If instead we check request.env[nil] it will be "set up warden".

This pull request fixes it so that the renderer will not produce the extra nil key, and to where request.env['warden'] will equal "set up warden"

Copy link
Member

@rafaelfranca rafaelfranca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the explanation. Could you add a test case and a CHANGELOG entry?

When rendering arbitrary templates, it is helpful to not overwrite `env` keys with nil if they don't match any found in the `RACK_KEY_TRANSLATION`

This allows the developer to set the environment to exactly what is needed for rendering.
@codeodor
Copy link
Contributor Author

Thanks @rafaelfranca, and done!

@eileencodes eileencodes merged commit 28ce091 into rails:master Oct 15, 2016
@eileencodes
Copy link
Member

Thanks @codeodor! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants